//
//  ImageView.swift
//  FileManager-NSCache-HttpDownload
//
//  Created by DaleGuo on 2023/9/19.
//

import SwiftUI

// 一张图片的view
struct ImageView: View {
//    @State var isLoading = true
//    @StateObject var vm = ImageViewModel(url: String)  //不能这样初始化，vm和v会同时初始化 在v的init中赋值vm即可
    @StateObject var vm: ImageViewModel

    init(url: String, key: String) {
        _vm = StateObject(wrappedValue: ImageViewModel(url: url, key: key))
    }

    var body: some View {
        if vm.isLoading {
            ProgressView()
        } else if let image = vm.image {
            Image(uiImage: image)
                .resizable()
                .clipShape(Circle())
        }
    }
}

struct ImageView_Previews: PreviewProvider {
    static var previews: some View {
        ImageView(url: "https://via.placeholder.com/600/92c952", key: "1")
            .frame(width: 75, height: 75)
            .previewLayout(.sizeThatFits)
    }
}
